<template>
  <div class="favorites-page">
    <van-button 
      type="danger" 
      size="small" 
      @click="cancelAllFavorites"
      class="cancel-all-btn"
    >
      全部取消收藏
    </van-button>
    
    <div 
      v-for="(item, index) in favorites" 
      :key="item.id || index" 
      class="product-card"
    >
      <van-image 
        :src="item.productPic" 
        width="100" 
        height="140" 
        fit="cover" 
        class="book-image"
      />
      
      <div class="book-info">
        <div class="title">{{ item.productTitle }}</div>
        <div class="author" v-if="item.productAuthor">作者：{{ item.productAuthor }}</div>
        <div class="price-section">
          <div class="current-price">￥{{ item.productPrice }}</div>
          <div class="original-price" v-if="item.productOriginalPrice">
            ￥{{ item.productOriginalPrice }}
          </div>
        </div>
        <div class="sales" v-if="item.sales">
          <van-icon name="eye-o" size="12" /> 已浏览 {{ item.sales }} 次
        </div>
      </div>
      
      <van-button 
        type="primary" 
        size="small" 
        @click="cancelFavorite(item, index)"
        class="cancel-favorite"
        color="#f44"
      >
        取消收藏
      </van-button>
    </div>
    
    <van-empty 
      description="暂无收藏书籍" 
      v-if="favorites.length === 0"
    />
  </div>
</template>

<script>
import request from '@/utils/request'
import { showToast, showConfirmDialog } from 'vant'

export default {
  data() {
    return { favorites: [], loading: false }
  },
  mounted() {
    this.fetchFavorites()
  },
  methods: {
    async fetchFavorites() {
      this.loading = true
      try {
        const { data } = await request.get('/favorite/listByMy')
        this.favorites = data || []
      } catch (error) {
        showToast(error?.message || '加载收藏失败')
      } finally {
        this.loading = false
      }
    },
    async cancelFavorite(item, index) {
      if (!item?.id) return
      try {
        await request.delete(`/favorite/${item.id}`)
        this.favorites.splice(index, 1)
        showToast('取消收藏成功')
      } catch (error) {
        showToast(error?.message || '取消收藏失败')
      }
    },
    async cancelAllFavorites() {
      showConfirmDialog({
        message: '确定清空全部收藏？'
      }).then(async () => {
        // 用户点击确认
        try {
          await request.delete('/favorite/deleteAllByMy')
          this.favorites = []
          showToast('全部取消收藏成功')
        } catch (error) {
          showToast(error?.message || '操作失败')
        }
      }).catch(() => {
        // 用户点击取消，不需要处理
      })
    }
  }
}
</script>

<style lang="less" scoped>

// 页面样式
.favorites-page {
  background-color: #f9f5ed;
  min-height: 100vh;
	padding-top: 46px;
}

.cancel-all-btn {
  margin-bottom: 10px;
}

.product-card {
  background-color: white;
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 10px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
  display: flex;
  align-items: center;
  padding: 10px;
}

.book-image {
  margin-right: 10px;
  border-radius: 4px;
}

.book-info {
  flex: 1;
}

.cancel-favorite {
  margin-left: 10px;
}

.title {
  font-size: 14px;
  line-height: 1.4;
  color: #333;
  margin-bottom: 5px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.author {
  font-size: 12px;
  color: #999;
  margin-bottom: 8px;
}

.price-section {
  display: flex;
  align-items: center;
  margin-bottom: 8px;

  .current-price {
    color: #f44;
    font-size: 16px;
    font-weight: bold;
    margin-right: 8px;
  }

  .original-price {
    color: #999;
    font-size: 12px;
    text-decoration: line-through;
  }
}

.sales {
  display: flex;
  align-items: center;
  font-size: 12px;
  color: #666;
}
</style>